from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from frame_work.logger import Logger
from frame_work.read_config import ReadConfig
from frame_work.setting import base_Dir, chrome_Driver_Path, ie_Driver_Path, firefox_Driver_Path
import time

logger = Logger(__name__).get_logger()


class BrowserEngine(object):

    # def __init__(self,url):
    #     self.url = url
    # 打开浏览器
    def open_browser(self):
        conf = ReadConfig()
        browser = conf.get_browser_name("browserCrHeadlessName")
        # browser = conf.get_browser_name("browserCrName")
        logger.debug("选择%s浏览器" % browser)
        # if self.user_Type == "admin":
        #     url = conf.get_Url( "admin_login_url")
        # else:
        #     url = conf.get_Url( "buyer_login_url")
        # logger.debug("判断选择管理员/商家/试客登录地址，登录地址为: %s" % url)

        if browser == "Firefox":
            self.driver = webdriver.Firefox()
            logger.debug("打开FireFox浏览器")
        elif browser == "Chrome":
            # options = webdriver.ChromeOptions()
            # options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
            chrome_options = Options()
            # 添加沙盒模式
            chrome_options.add_argument("--no-sandbox")
            chrome_options.add_argument('--disable-dev-shm-usage')
            chrome_options.add_argument('ignore-certificate-errors')
            self.driver = webdriver.Chrome(
                chrome_Driver_Path, chrome_options=chrome_options)
            # url = 'https://www.baidu.com'
            # url = "https://api.m.jd.com/client.action?functionId=lite_newBabelAwardCollection&body={%22activityId%22:%22TqTRGRrp9HZTfeyRTL2UGmX4mHG%22,%22scene%22:%221%22,%22args%22:%22key=DD98BDDD838A0F60578BEC06CF63F1E027D176A315E60F18E48104FE15076D58FFA18701879AA339876F5FB4B7F513F9_bingo,roleId=9E2C6D36242771388D05258F238C6CD4_bingo,strengthenKey=19F512DCD8EE34ABE9C4FB4A92C2F42AD9E7D4E209C7E54FA67BF30135A95927_bingo%22,%22platform%22:%221%22,%22orgType%22:%222%22,%22openId%22:%22-1%22,%22pageClickKey%22:%22-1%22,%22eid%22:%22%22,%22fp%22:%22%22,%22userArea%22:%22-1%22,%22client%22:%22-1%22,%22clientVersion%22:%22-1%22,%22uuid%22:%22-1%22,%22osVersion%22:%22-1%22,%22brand%22:%22-1%22,%22model%22:%22-1%22,%22networkType%22:%22-1%22,%22pageClick%22:%22Babel_Coupon%22,%22couponSource%22:%22manual%22,%22couponSourceDetail%22:%22-100%22,%22channel%22:%22%E9%80%9A%E5%A4%A9%E5%A1%94%E4%BC%9A%E5%9C%BA%22,%22cv%22:%222.0%22}&client=wh5&clientVersion=1.0.0"
            # self.driver.get(url)
            # for i in range(0,100):
            #     self.driver.refresh()
            # time.sleep(3600)
            logger.debug("打开Chrome浏览器")
        elif browser == "ChromeHeadless":
            chrome_options = Options()
            # 无头模式启动
            chrome_options.add_argument('--headless')
            # 谷歌文档提到需要加上这个属性来规避bug
            chrome_options.add_argument('--disable-gpu')
            chrome_options.add_argument('blink-settings=imagesEnabled=false')
            # 取消CMD控制台DevTools listening on ws://127.0.0.1...提示的方法
            chrome_options.add_experimental_option(
                'excludeSwitches', ['enable-logging'])
            # 添加沙盒模式
            chrome_options.add_argument("--no-sandbox")
            chrome_options.add_argument('--disable-dev-shm-usage')
            chrome_options.add_argument('ignore-certificate-errors')
            # 初始化实例
            self.driver = webdriver.Chrome(
                executable_path=chrome_Driver_Path,
                chrome_options=chrome_options)
            # self.driver = webdriver.Chrome(chrome_options=chrome_options)
            logger.debug("打开Chrome 无界面静默模式运行")
        elif browser == "IE":
            self.driver = webdriver.Ie(ie_Driver_Path)
            logger.debug("打开Ie浏览器")
        elif browser == "PhantomJs":
            self.driver = webdriver.PhantomJS()
            logger.debug("打开PhantomJS浏览器")

        # self.driver.get(self.url)
        # print(self.driver.page_source)
        # logger.debug("打开url: %s" % self.url)
        # # self.driver.set_window_size(1920, 1080)
        # self.driver.maximize_window()
        # logger.debug("浏览器窗口最大化")
        # self.driver.implicitly_wait(10)
        # logger.debug("Set implicitly wait 10 seconds.")
        return self.driver

    def quit_browser(self):
        logger.debug("关闭退出浏览器")
        # self.driver.close()
        self.driver.quit()


if __name__ == '__main__':
    BrowserEngine1 = BrowserEngine()
    BrowserEngine1.open_browser()
    BrowserEngine1.quit_browser()
